package main

import (
    "net/http"
    "fmt"
    "encoding/json"
    "strings"
    "io/ioutil"
    "crypto/tls"
)

type ExecDataPro struct {

    Pid int `json:"pid"`
}

type ExecData struct {
    Data ExecDataPro `json:"data"`
}

type ExecStatusDataPro struct {

    Exited int `json:"exited"`
}

type ExecStatusData struct {
    Data ExecStatusDataPro `json:"data"`
}

func post(url string, post_data string) int {

    reader := strings.NewReader(post_data)
    request, err := http.NewRequest("POST", url, reader)
    if err != nil {
        fmt.Println(err.Error())
        return -1
    }
    /* 加head 此处cookie,CSRFPreventionToken不需要加冒号*/

    request.Header.Set("cookie", g_final_ticket)
    request.Header.Add("CSRFPreventionToken", g_final_token)

    //curl -k
    tr := &http.Transport{
        TLSClientConfig:&tls.Config{InsecureSkipVerify: true},
    }
    client := &http.Client{Transport: tr}

    resp, err := client.Do(request)
    if err != nil {
        fmt.Println(err.Error())
        return -1
    }
    
    defer resp.Body.Close()

    resp_bytes, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        fmt.Println(err.Error())
        return -1
    }
    
    //fmt.Printf("%s\n\n", resp_bytes)

    if strings.Contains(url, "/agent/exec") {
        var exec_data ExecData
        err := json.Unmarshal(resp_bytes, &exec_data) 
        if err != nil {
            fmt.Println(err.Error())
            return -1
        }

        //fmt.Println(exec_data.Data.Pid)
        url_new := strings.Replace(url, "/agent/exec", "/agent/exec-status?pid=", -1)
        url_new_get := fmt.Sprintf("%s%d", url_new, exec_data.Data.Pid)
        //fmt.Println(url_new_get)
        ret_get := get(url_new_get)
        if ret_get == -1 {
            fmt.Println("/agent/exec-status error")
            return -1
        }
    }

    return 0;
}

func get(url string) int {

    request, err := http.NewRequest("GET", url, nil)
    if err != nil {
        fmt.Println(err.Error())
        return -1
    }
    /* 加head 此处cookie,CSRFPreventionToken不需要加冒号*/

    request.Header.Set("cookie", g_final_ticket)
    request.Header.Add("CSRFPreventionToken", g_final_token)

    //curl -k
    tr := &http.Transport{
        TLSClientConfig:&tls.Config{InsecureSkipVerify: true},
    }
    client := &http.Client{Transport: tr}

    resp, err := client.Do(request)
    if err != nil {
        fmt.Println(err.Error())
        return -1
    }
    
    defer resp.Body.Close()

    resp_bytes, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        fmt.Println(err.Error())
        return -1
    }
        
    var execstatus_data ExecStatusData
    err_status := json.Unmarshal(resp_bytes, &execstatus_data) 
    if err_status != nil {
        fmt.Println(err.Error())
        return -1
    }

    //fmt.Println(execstatus_data.Data.Exited)
    //如果没执行完，就继续执行
    if execstatus_data.Data.Exited == 0 {
        get(url)
    } else {
        //fmt.Printf("%s\n\n", resp_bytes)
        f.Write([]byte(resp_bytes))
    }

    return 0
}
